package com.ifcar.app.db;

import java.util.Properties;

import com.ifcar.app.util.ProUtil;
import com.mongodb.DB;
import com.mongodb.Mongo;

/**
 * 数据库工具类
 * @author nxlixa
 *
 */
public class DBUtil {
	// dbUrl key from properties
	private static final String DBURL_KEY = "dbUrl";
	// default dburl
	private static final String DBURL_DEFAULT = "localhost";
	// dbUrl key from properties
	private static final String DBPORT_KEY = "dbPort";
	// default port
	private static final String DBPORT_DEFAULT = "27017";
	
	/**
	 * db NAME
	 */
	private static final String DB_NAME = "smartrider_db";
	
	/**
	 * db connection
	 */
	private static Mongo mConn;
	
	/**
	 * db 
	 */
	public static DB mDb;
	
	/**
	 * ==================== collections ===================== 
	 */
	// users_base 用户基本信息表
	public static final String COLLECTION_USERS_BASE = "users_base";
	// users_info 用户信息表
	public static final String COLLECTION_USERS_INFO = "users_info";
	// smarthelp_question 智能帮助系统 问题表
	public static final String COLLECTION_SMARTHLEP_QUESTION = "smarthelp_question";
	// smarthelp_answer 智能帮助系统 答案表
	public static final String COLLECTION_SMARTHLEP_ANSWER = "smarthelp_answer";
	// publish_info 信息发布表
	public static final String COLLECTION_PUBLISH_INFOR = "publish_info";
	// chat_message 信息表【私信，聊天等记录】
	public static final String COLLECTION_CHAT_MESSAGE = "chat_message";
	// system_message 系统通知信息记录
	public static final String COLLECTION_SYSTEM_MESSAGE = "system_message";
	// users_trade 用户交易表
	public static final String COLLECTION_USERS_TRADER = "users_trade";
	// relation_info 用户关系表 粉丝、互粉、拉黑信息记录
	public static final String COLLECTION_RELATION_INFO = "relation_info";
	
	/**
	 * 初始化数据库连接
	 * @return
	 */
	static {
		try {
			if(null == mConn){
				Properties pro = new ProUtil().getConfigProperties();
				String dburl = pro.getProperty(DBURL_KEY, DBURL_DEFAULT);
				int dbport = Integer.parseInt(pro.getProperty(DBPORT_KEY, DBPORT_DEFAULT));
				
				mConn = new Mongo(dburl, dbport);
			}
			if(null != mConn && null == mDb){
				mDb = mConn.getDB(DB_NAME);
			}
		} catch (Exception e) {
			mConn = null;
			mDb = null;
			e.printStackTrace();
		} 
		
	}
	
}
